$(function($) {
	$('#create').click(function() {
		
		$('#qrcode').empty();
		
		var dono = $('#dono').val();
		var lotno = $('#lotno').val();
		var item = $('#item').val();
		var supplier = $('#supplier').val();
		var qty = $('#qty').val();
		
		if (dono == "" || lotno == "" || item == "" || supplier == "" || qty == "") {
			$.showInfo("以上选项都必须填写！");
			return;
		}
		
		var text = dono + space(11) + lotno + space(14) + item + space(11) + supplier + space(4) + qty;
		
		$('#qrcode').qrcode({
			width: 180,
			height: 180,
			text: text
		});
	});
	
	$('#print').click(function() {
		window.print();
	});
	
	$('#reset').click(function() {
		$('#dono, #lotno, #item, #supplier, #qty').val("");
	});
});

function space(num) {
	var val = "";
	for (var i=0; i<num; i++) {
		val += " ";
	}
	return val;
}

function toUtf8(str) {   
    var out, i, len, c;   
    out = "";   
    len = str.length;   
    for(i = 0; i < len; i++) {   
    	c = str.charCodeAt(i);   
    	if ((c >= 0x0001) && (c <= 0x007F)) {   
        	out += str.charAt(i);   
    	} else if (c > 0x07FF) {   
        	out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));   
        	out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));   
        	out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));   
    	} else {   
        	out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));   
        	out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));   
    	}   
    }   
    return out;   
}